package com.haiqi.modules.report.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.VoyageReport;
import com.haiqi.common.utils.DateUtils;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.report.service.VoyageReportService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by hythzx on 2016/5/23.
 * 航次计划报告
 */

@Controller
@RequestMapping("/manage/report/voyage/report")
@SafeAuthority(menuCode = "voyageReport,monitorLocation")
public class VoyageReportController extends BaseController {


    @Autowired
    private VoyageReportService voyageReportService;

    /**
     * 本菜单的code
     */
    private String menuCode = "voyageReport";

    @RequestMapping(value = "list")
    public String list(VoyageReport voyageReport, PageBean page, Model model, HttpSession session){
        generateMenuCode(session,menuCode);
        ResponseBean.UserBean user = getUser(session);
        voyageReport.setCompany(user.getOfficeid());
        PageInfo<VoyageReport> voyageReportList = voyageReportService.getList(voyageReport, page);
        model.addAttribute("pageInfo",voyageReportList);
        model.addAttribute("param",voyageReport);
        return "/modules/report/voyageReport";
    }

    @RequestMapping(value = "ajax/list")
    @ResponseBody
    public DataGridBean<VoyageReport> ajaxList(VoyageReport voyageReport, PageBean page, Model model, HttpSession session,String company){
        if(StringUtils.isNotEmpty(company)){
            voyageReport.setCompany(company);
        }
        PageInfo<VoyageReport> voyageReportList = voyageReportService.getList(voyageReport, page);
        DataGridBean<VoyageReport> dataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(voyageReportList, dataGridBean);
        if(dataGridBean.getRows() != null){
            for (VoyageReport report : dataGridBean.getRows()) {
                report.setShipName(report.getShip().getShipName());
                if(report.getDateReport() != null){
                    report.setReportDateStr(DateUtils.dateToString(report.getDateReport(), DateUtils.DATE_FORMAT));
                }
            }
        }
        return dataGridBean;
    }

}
